<?php

namespace think\wework\Api;

use think\wework\Message\Receiver;
use think\wework\Abstracts\WeworkApiWithToken;
use think\wework\Message\ResponseMessageInterface;

class Message extends WeworkApiWithToken
{
    private static ?Message $instance = null;
    public static function getInstance(): Message
    {
        if(!self::$instance instanceof Message) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 发送应用消息
     * @param Receiver $receiver
     * @param ResponseMessageInterface $responseMessage
     * @param bool $safe
     * @return array
     */
    public function send(Receiver $receiver, ResponseMessageInterface $responseMessage, bool $safe = false): array
    {
        $json = array_merge(
            ['agentid' => $this->agentId],
            $receiver->get(),
            $responseMessage->formatForResponse(),
            ['safe' => (int)$safe]
        );
        return $this->httpClient->post('message/send', compact('json'))->toArray();
    }
}
